package com.cq.hd.app.controller;

import com.cq.hd.common.annotation.LoginUser;
import com.cq.hd.common.entity.AppLoginUser;
import com.cq.hd.common.entity.BusinessInfo;
import com.cq.hd.common.enums.biz.PayTypeEnum;
import com.cq.hd.common.pagination.Page;
import com.cq.hd.common.response.Result;
import com.cq.hd.common.response.Throw;
import com.cq.hd.member.api.TbMemberFeeConfigApi;
import com.cq.hd.member.api.dto.*;
import com.cq.hd.member.api.vo.AppMemberFeeConfigPageVo;
import com.cq.hd.member.api.vo.AppMemberFeeConfigPayVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping("/merchantFee")
@Api(tags = "会员开通/续费相关接口")
public class MemberFeeController {

    @Autowired
    private TbMemberFeeConfigApi memberFeeConfigApi;

    @ApiOperation(value = "会员套餐配置列表")
    @ApiResponse(response = AppMemberFeeConfigPageVo.class, code = 200, message = "接口返回对象参数")
    @GetMapping("/page")
    public Result<Page<AppMemberFeeConfigPageVo>> pageAppMemberFeeConfig(AppMemberFeeConfigPageDto appMemberFeeConfigPageDto) {
        return memberFeeConfigApi.pageAppMemberFeeConfig(appMemberFeeConfigPageDto);
    }

    // 创建充值订单，返回充值订单号
    @ApiOperation(value = "创建订单，返回充值订单号")
    @ApiImplicitParam(name = "appMemberFeeConfigCreateDto", value = "实体", required = true, paramType = "body", dataType = "AppMemberFeeConfigCreateDto")
    @PostMapping("/createOrder")
    public Result<String> createOrder(@RequestBody AppMemberFeeConfigCreateDto appMemberFeeConfigCreateDto,
                                      @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        Integer payType = appMemberFeeConfigCreateDto.getPayType();
        if (payType == null || PayTypeEnum.typeOf(payType) == null) {
            Throw.isBusinessException("请选择支付方式");
        }

        if (!PayTypeEnum.WX.getValue().equals(payType)) {
            // 网银对公打款，返回商家后台访问地址
            Throw.isBusinessException("请前往PC端登录商家后台进行操作");
        }

        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            appMemberFeeConfigCreateDto.setBusinessId(businessInfo.getId());
        }
        appMemberFeeConfigCreateDto.setUserId(appLoginUser.getUserId());
        return memberFeeConfigApi.createAppMemberFeeConfigOrder(appMemberFeeConfigCreateDto);
    }

    // 立即支付，唤起第三方支付
    @ApiOperation(value = "立即支付，唤起第三方支付")
    @ApiImplicitParam(name = "appMemberFeeConfigPayDto", value = "实体", paramType = "body", dataType = "AppMemberFeeConfigPayDto")
    @ApiResponse(response = AppMemberFeeConfigPayVo.class, code = 200, message = "接口返回对象参数")
    @PostMapping("/pay")
    public Result<AppMemberFeeConfigPayVo> pay(@RequestBody AppMemberFeeConfigPayDto appMemberFeeConfigPayDto, @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            appMemberFeeConfigPayDto.setBusinessId(businessInfo.getId());
        }
        appMemberFeeConfigPayDto.setUserId(appLoginUser.getUserId());
        return memberFeeConfigApi.payAppMemberFeeConfigOrder(appMemberFeeConfigPayDto);
    }

    @ApiOperation(value = "查单（只要唤醒了第三方支付，关闭第三方支付页面之后，不管是成功还是失败，都要调用）")
    @ApiImplicitParam(name = "appMemberFeePayQueryDto", value = "实体", paramType = "body", dataType = "AppMemberFeePayQueryDto")
    @PostMapping("/queryOrderPay")
    public Result<Boolean> queryOrderPay(@RequestBody AppMemberFeePayQueryDto appMemberFeePayQueryDto, @ApiIgnore @LoginUser AppLoginUser appLoginUser) {
        BusinessInfo businessInfo = appLoginUser.getUserInfo().getBusinessInfo();
        if (businessInfo != null) {
            appMemberFeePayQueryDto.setBusinessId(businessInfo.getId());
        }
        appMemberFeePayQueryDto.setUserId(appLoginUser.getUserId());
        return memberFeeConfigApi.queryOrderPay(appMemberFeePayQueryDto);
    }

}
